package com.tebiecloud.order.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.order.client.model.entity.OrderStats1;
import com.tebiecloud.order.client.service.IStats1ServiceClient;
import com.tebiecloud.order.server.service.OrderStats1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/feign/orderstats1")
public class Stats1Controller implements IStats1ServiceClient {

    @Autowired
    OrderStats1Service orderStats1Service;

    @PostMapping("listPage")
    @Override
    public ResultBody<Page<OrderStats1>> statsListPage(@RequestParam("page") int page, @RequestParam("limit") int limit,
                                                       @RequestParam("storeId") long storeId, @RequestParam(value = "keyword",required = false) String keyword) {
        QueryWrapper<OrderStats1> orderStats1QueryWrapper = new QueryWrapper<>();
        orderStats1QueryWrapper.eq("store_id",storeId);
        if(StringUtils.isNotEmpty(keyword)){
            orderStats1QueryWrapper.and(Wrapper->Wrapper.like("nickname",keyword).or().like("phone",keyword));
        }
        IPage<OrderStats1> orderStats1IPage = orderStats1Service.page(new Page<OrderStats1>(page,limit),orderStats1QueryWrapper);
        return ResultBodyUtils.ok(orderStats1IPage);
    }

    /**
     * 获取会员信息
     * @param unionid
     * @param storeId
     * @return
     */
    @PostMapping("statsInfo")
    @Override
    public ResultBody<OrderStats1> statsInfo(@RequestParam("unionid") String unionid,
                                                   @RequestParam("storeId") long storeId) {
        QueryWrapper<OrderStats1> orderStats1QueryWrapper = new QueryWrapper<>();
        orderStats1QueryWrapper.eq("store_id",storeId);
        orderStats1QueryWrapper.eq("unionid",unionid);
        OrderStats1 orderStats1 = orderStats1Service.getOne(orderStats1QueryWrapper);
        return ResultBodyUtils.ok(orderStats1);
    }

    /**
     * 更新会员备注
     * @param unionid
     * @param storeId
     * @param remarkName
     * @return
     */
    @PostMapping("statsUpdate")
    @Override
    public ResultBody statsUpdate(
            @RequestParam("unionid") String unionid,
            @RequestParam("storeId") long storeId,
            @RequestParam("remarkName") String remarkName
    ) {
        OrderStats1 orderStats1 = new OrderStats1();
        orderStats1.setRemarkName(remarkName);
        UpdateWrapper<OrderStats1> orderStats1UpdateWrapper = new UpdateWrapper<>();
        orderStats1UpdateWrapper.eq("store_id",storeId);
        orderStats1UpdateWrapper.eq("unionid",unionid);
        orderStats1Service.update(orderStats1,orderStats1UpdateWrapper);
        return ResultBodyUtils.ok();
    }
}
